package com.scu.qingshi.listener;

import com.google.common.eventbus.Subscribe;
import com.scu.qingshi.config.cummunity.CommunityConfig;
import com.scu.qingshi.config.cummunity.CommunityFactory;
import com.scu.qingshi.domain.email.model.AuditResultEmailMessage;
import com.scu.qingshi.domain.interview.event.AuditSuccessEvent;
import com.scu.qingshi.infrastructure.dao.po.InterviewProcess;
import com.scu.qingshi.infrastructure.dao.po.User;
import com.scu.qingshi.infrastructure.dao.service.InterviewProcessService;
import com.scu.qingshi.infrastructure.dao.service.UserService;
import com.scu.qingshi.infrastructure.event.EventBusCenter;
import com.scu.qingshi.infrastructure.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 *
 * @author qingshi
 */
@Component
public class AuditEventListener {

    @Autowired
    EmailUtil emailUtil;

    @Autowired
    UserService userService;

    @Autowired
    InterviewProcessService interviewProcessService;

    @Autowired
    EventBusCenter eventBusCenter;

    @PostConstruct
    void init(){
        eventBusCenter.register(this);
    }

    @Subscribe
    void listenAuditResult(AuditSuccessEvent event){

        User user = userService.getById(event.getUserId());
        CommunityConfig community = CommunityFactory.getCommunity(event.getOpenId());

        InterviewProcess process = interviewProcessService.getById(event.getInterId());

        emailUtil.sendEmail(community.getEmail(),
                user.getEmail(),
                AuditResultEmailMessage.getTitle(community.getName()),
                AuditResultEmailMessage.getContent(event.getAuditResult(),process.getName())
        );

    }


}
